package com.yfuxdeer.youchat.utils;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yfuxdeer.youchat.pojo.User;

public class StorageUtil {
    public static User readUser(Context context) {
        String string = context.getSharedPreferences("user", MODE_PRIVATE).getString("loginUser", "");
        User user;
        try {
            user = new ObjectMapper().readValue(string, User.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
        return user;
    }

    public static void writeUser(Context context, User user) {
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        try {
            editor.putString("loginUser", new ObjectMapper().writeValueAsString(user));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        editor.commit();
        user.setPassword("");
    }
}
